<%--
  Created by IntelliJ IDEA.
  User: lujw
  Date: 2017/6/16
  Time: 8:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/hospital_token.jsp"%>
<%@ include file="/common/hospital_notice.jsp"%>
<%
    //通知详情
    Integer noticeId = input.getInt("notice_id", -1);
    List<Map> list = QBuilder.db("basedb").select("new map(n.id as id, n.operatorId as operator_id, n.title as title, n.content as content, n.isComment as is_comment, n.author as author, n.attachment as attachment, concat(from_unixTime(n.cTime/1000, '%Y-%m-%d %H:%i:%s'), '') as time, c.name as category_name, l.name as level_name, l.jsonString as json, count(f.readTime) as read_num, count(f.id) as total_num)").from("HospitalNotice n, HospitalNoticeCategory c, HospitalNoticeLevel l, HospitalNoticeFeedback f").where(new Where("n.status=", ConstantUtil.STATUS_ENABLE).and("n.hospital=", hospital).and("c.status=", ConstantUtil.STATUS_ENABLE).and("c.hospital=", NOTICE_CATEGORY_HOSPITAL).and("l.status=", ConstantUtil.STATUS_ENABLE).and("l.hospital=", hospital).and("n.id=", noticeId).and("n.category=c.id").and("n.level=l.id").and("f.noticeId=n.id").and("f.status=", ConstantUtil.STATUS_ENABLE)).group("n.id").page(1, 1).query().getResultObj();
    if (list.size() == 0) {
        msg.put("data", Collections.emptyMap());
        ErrcodeUtil.setErrcode(msg, ErrcodeUtil.NO_RESULT);
        Tools.encryptPrint(out, msg);
        return;
    }
    Map map = list.get(0);
    Map jsonMap = JSONUtil.mapFromJson(Tools.getDesc(map.remove("json"), ""));
    if (Objects.nonNull(jsonMap))
        map.put("level_color", Tools.getDesc(jsonMap.get("color"), ""));
    else
        map.put("level_color", "");
    List attachList = JSONUtil.listFromJson(Tools.getDesc(map.remove("attachment"), "[]"));
    if (Objects.nonNull(attachList) && attachList.size() > 0) {
        for (int j = 0, aLength = attachList.size(); j < aLength; j++) {
            Map attachMap = (Map)attachList.get(j);
            String urlFileName = Tools.getDesc(attachMap.get("url"), "");
            int index;
            if ((index = urlFileName.lastIndexOf("/")) > -1) {
                String fileName = urlFileName.substring(index + 1);
                urlFileName = urlFileName.substring(0,urlFileName .lastIndexOf("/")) + "/" + URLEncodeUtil.encodeURL(fileName);
                attachMap.put("url", urlFileName);
            }
        }
    } else {
        attachList = Collections.EMPTY_LIST;
    }
    map.put("attachment", attachList);
    if ((Integer)map.get("is_comment") == 1) {
        List commentList = QBuilder.db("basedb").select("count(id)").from("HospitalNoticeComment").where(new Where("hospital=", hospital).and("status=", ConstantUtil.STATUS_ENABLE).and("noticeId=", noticeId)).query().getResultObj();
        map.put("comment_num", commentList.get(0) + "");
    } else {
        //如果这条通知不能评论，直接置为0
        map.put("comment_num", 0);
    }

    List<HospitalNoticeFeedback> feedbackList = QBuilder.db("basedb").select().from("HospitalNoticeFeedback").where(new Where("hospital=", hospital).and("noticeId=", noticeId).and("status=", ConstantUtil.STATUS_ENABLE).and("userId=", token.getUid())).query().getResultObj();
    HospitalNoticeFeedback feedback;
    if (feedbackList.size() == 0 || Objects.isNull((feedback = feedbackList.get(0)).getReadTime()))
        map.put("read_time", "");
    else
        map.put("read_time", feedback.getReadTime());
    msg.put("data", map);
    Tools.encryptPrint(out, msg);
%>
